# -*- coding:utf-8 -*-
from . import app
from . import db
from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired)
from flask_login import current_user


__all__ = [ 'User', 'Orders', 'EmailQueue', 'News', 'Site']

ROLES = app.config['ROLES']
# ROLES = ['admin', 'senior', 'ordinary']

class User(db.Document, UserMixin):
    '''
    用户：
    1. 管理员
    2. 普通用户
    3. 高级用户
    '''
    meta = {
        'collection': 'user',
        'ordering': ['-id'],
        'strict': True
    }
    email = db.StringField(unique=True)
    nickname = db.StringField()
    password_hash = db.StringField()
    postemails = db.ListField() # 接收新闻推送的邮箱
    identity = db.StringField() # 身份 { 1:管理员, 2:普通用户, 3:高级用户 }

    #密码加密
    def hash_password(self, password):
        self.password_hash = generate_password_hash(password)

    #验证密码
    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    #使用username作为令牌
    def generate_auth_token(self, expiration = 600):
        s = Serializer(app.config['SECRET_KEY'], expires_in = expiration)
        return s.dumps({ 'username':self.username })

    @staticmethod
    def verify_auth_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None
        except BadSignature:
            return None
        user = User.objects(username=data['username']).first()
        return user

class Orders(db.Document):
    '''
    用户的订单
    url：抓取的对象连接
    email：订阅该链接的用户邮箱
    add_time:添加时间
    '''
    meta = {
        'collection': 'orders',
        'ordering': ['-id'],
        'strict': True
    }

    site_url = db.StringField()
    site_title = db.StringField()
    user_email = db.ListField(db.StringField()) #用户邮箱
    add_time = db.DateTimeField(default=datetime.now())

class EmailQueue(db.Document):
    '''
    email发送记录
    content:
    '''
    meta = {
        'collection': 'sendemail',
        'ordering': ['id'],
        'strict': True
    }

    contents = db.ListField()
    post_emails = db.StringField() #单一用户记录
    status = db.StringField()
    send_time = db.DateTimeField(default=datetime.now())

class News(db.Document):
    '''
    抓取的内容
    url:抓取对象的链接
    content:捕获的内容
    update_time:捕获的时间
    '''

    meta = {
        'collection': 'newest',
        'ordering': ['-id'],
        'strict': True
    }
    site_url = db.StringField()
    content = db.ListField()
    update_time = db.DateTimeField(default=datetime.now())


class Site(db.Document):
    '''
    目标网站：
    title:网站名称
    url:网站链接
    status：网站状态
    '''

    meta = {
        'collection': 'site',
        'ordering': ['-id'],
        'strict': True
    }
    title = db.StringField()
    url = db.StringField(unique=True)
    status = db.StringField()



